package com.longrise.android.by.javascriptInterface;

import android.content.Intent;
import android.net.Uri;
import android.webkit.JavascriptInterface;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.ShareParams;
import com.longrise.android.byjk.permission.PermissionUtil;
import com.longrise.common.utils.AppUtil;
import com.longrise.android.by.activity.BYWebActivity;
import com.longrise.android.by.utils.PhoneContactHelper;


/**
 * Created by godliness on 2017/3/20.
 */

public class ClictMInterface {

    private BYWebActivity mCxt;

    public ClictMInterface(BYWebActivity activity) {
        this.mCxt = activity;
    }

    @JavascriptInterface
    public void shareParams(String title, String content, String url, String iconUrl) {
        if (mCxt != null) {
            ShareParams shareParams = new ShareParams();
            shareParams.mUrl = url;
            shareParams.mContent = content;
            shareParams.mTitle = title;
            shareParams.mHeadUrl = iconUrl;
            mCxt.showShareDialog(shareParams);
        }
    }

//    /**
//     * 获取联系人  这个要改 详情见ClientManagerActivity xkj20170718
//     */
//    @JavascriptInterface
//    public void getContact() {
//        mCxt.mHandler.post(new Runnable() {
//            @Override
//            public void run() {
//                toGetContact();
//            }
//        });
//    }

    @JavascriptInterface
    public void loadFinish() {
        mCxt.mHandler.post(new Runnable() {
            @Override
            public void run() {
                mCxt.isLoading(false);
            }
        });
    }

    /**
     * 打电话
     *
     * @param phoneNum 电话号码
     */
    @JavascriptInterface
    public void toCall(final String phoneNum) {
        mCxt.mHandler.post(new Runnable() {
            @Override
            public void run() {
                toCallPhone(phoneNum);
            }
        });
    }

    /**
     * 获取手机联系人
     */
    private void toGetContact() {
        PermissionUtil.getInstance().requestPermission(mCxt, PermissionUtil.Permission.CONTACTS, AppUtil.getString(R.string.contact_permission), AppUtil.getString(R.string.contact_permission_hint), new PermissionUtil.PermissionCallBack() {
            @Override
            public void onSuccess() {
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            PhoneContactHelper helper = new PhoneContactHelper();
                            final String contact = helper.getContact(mCxt);
                            mCxt.mWebView.post(new Runnable() {
                                @Override
                                public void run() {
                                    mCxt.mWebView.loadUrl("javascript:$_$.dealResult('" + contact + "')");
                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }

            @Override
            public void onDenied() {

            }
        });
    }

    /**
     * 拨打电话
     *
     * @param phoneNum 电话号码
     */
    private void toCallPhone(final String phoneNum) {
        PermissionUtil.getInstance().requestPermission(mCxt, PermissionUtil.Permission.PHONE, AppUtil.getString(R.string.call_permission), AppUtil.getString(R.string.call_permission_hint), new PermissionUtil.PermissionCallBack() {
            @Override
            public void onSuccess() {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNum));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mCxt.startActivity(intent);
            }

            @Override
            public void onDenied() {

            }
        });
    }
}
